//
//  PaymentListView.swift
//  SmartHousekeeper
//
//  Created by 张磊 on 2020/2/22.
//  Copyright © 2020 LS. All rights reserved.
//

import UIKit
func changeMoneyAttribute(_ money: String) -> (NSMutableAttributedString){
    let myAttrString = NSMutableAttributedString(string: money)
    let range = NSRange(location: myAttrString.length - 1, length: 1)
    myAttrString.addAttribute(NSAttributedString.Key.font, value: UIFont.boldSystemFont(ofSize: 15), range: range)
    return myAttrString
}
class PaymentListView: UIView, UITableViewDelegate,UITableViewDataSource{
    
    
    typealias PropertyExpensesListViewSwitchBlock = (_ isOn:Bool)->()
    public var switchBlock : PropertyExpensesListViewSwitchBlock? = nil
    
    typealias PaymentListViewPayBlock = ()->()
    public var paymentBlock : PaymentListViewPayBlock? = nil
    
    
    
    lazy var paymentBtn : UIButton = {
        let value = UIButton(textColor: .white, textFont: UIFont.boldSystemFont(ofSize: Adapter(20)))
        value.setTitle("支付", for: .normal)
        value.layer.cornerRadius = Adapter(7)
        value.layer.masksToBounds = true
        value.backgroundColor =  RGBColor(r: 62, g: 134, b: 230, a: 1)
        return value
    }()
    
    
    
    lazy var tablewView : UITableView = {
        let value = UITableView(frame: CGRect.zero, style:.grouped)
        value.delegate = self
        value.dataSource = self
        value.separatorStyle = .none
        value.backgroundColor = .clear
        value.bounces = false
        return value
    }()
    var array =
        [["payType":"csBank","img":"icon_csbank","isSelect":"1"],
         ["payType":"csBank","img":"icon_csbank","isSelect":"0"],
         ["payType":"csBank","img":"icon_csbank","isSelect":"0"],
    ]
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    func setUI() {
        self.addSubview(tablewView)
        tablewView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-Adapter(135))
        }
        let company = UILabel()
        company.textColor = RGBColor(r: 116, g: 116, b: 116, a: 1)
        company.font = UIFont.systemFont(ofSize: Adapter(12))
        company.text = "技术提供商:上海领美软件科技有限公司"
        self.addSubview(company)
        company.snp.makeConstraints { (make) in
                  make.centerX.equalToSuperview()
                  make.top.equalTo(tablewView.snp.bottom).offset(Adapter(15))
                  make.height.equalTo(Adapter(20))
              }
        
        
        self.addSubview(paymentBtn)
        paymentBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(30))
            make.right.equalToSuperview().offset(-Adapter(30))
            make.top.equalTo(company.snp.bottom).offset(Adapter(15))
            make.height.equalTo(Adapter(45))
        }
        paymentBtn.sce.add(.touchUpInside) { (btn) in
            self.paymentBlock?()
        }
        
        
    }
    
    //初始化数据源
    private func allInitSelectCell() {
        var arr = Array<Any>()
        for dic in array {
            var dicMU = dic
            dicMU.updateValue("0", forKey: "isSelect")
            arr.append(dicMU)
        }
        array = arr as! [[String : String]]
        tablewView.reloadData()
    }
    
    //立即支付
    @objc func paymentAction() {
        self.paymentBlock?()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return Adapter(80)
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        var dic = array[indexPath.row]
        guard dic["isSelect"] == "0" else {
            return
        }
        allInitSelectCell()
        dic.updateValue("1", forKey: "isSelect")
        
        array[indexPath.row] = dic
        tableView.reloadData()
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "PaymentViewTableViewCell") as? PaymentViewTableViewCell
        if cell == nil{
            cell = PaymentViewTableViewCell.init(style: .default, reuseIdentifier: "PaymentViewTableViewCell")
        }
        let dic = array[indexPath.row]
        
        cell?.imgLogo.image = UIImage(named: dic["img"]!)
        if dic["isSelect"] == "1" {
            cell?.imgSelect.image = UIImage(named: "icon_success")
        } else {
            cell?.imgSelect.image = UIImage(named: "icon_no_select")
        }
        cell?.selectionStyle = .none
        cell?.backgroundColor = .clear
        return cell!
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return Adapter(220)
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = PaymentViewTableViewHeader(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: Adapter(220)))
        header.moneyL.attributedText = changeMoneyAttribute("800.00元")
        return header
    }
    
}
